package com.md.api.controller;

import com.md.api.annotation.NoRepeatSubmit;
import com.md.api.common.utils.RedisUtil;
import com.md.api.common.utils.StringUtils;
import com.md.api.entity.*;
import com.md.api.entity.dto.ComposeQueryDto;
import com.md.api.service.UserSevice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RequestMapping("/currentUser")
@RestController
public class UserController {

	@Resource
	UserSevice userSevice;




	@RequestMapping("/info")
	public HttpResult info(HttpServletRequest request) {

		return userSevice.getInfo(request);

	}


	@NoRepeatSubmit
	@RequestMapping("/register")
	public HttpResult register(HttpServletRequest request, @RequestBody UserInfo userInfo) {
		String language = request.getHeader("language");
		if (StringUtils.isEmpty(userInfo.getEmail())){
			if(StringUtils.isNotEmpty(language)&&language.equals("en")){
				return HttpResult.fail("Email cannot be empty");
			}
			return HttpResult.fail("郵箱不能爲空！");
		}
		if (StringUtils.isEmpty(userInfo.getPhone())){
			if(StringUtils.isNotEmpty(language)&&language.equals("en")){
				return HttpResult.fail("Phone cannot be empty");
			}
			return HttpResult.fail("電話不能爲空！");
		}
		if (null==userInfo.getUserType()){
			if(StringUtils.isNotEmpty(language)&&language.equals("en")){
				return HttpResult.fail("User type cannot be empty");
			}
			return HttpResult.fail("用戶類型不能爲空！");
		}
		return userSevice.register(request,userInfo);

	}

	@NoRepeatSubmit
	@RequestMapping("/updateBaseInfo")
	public HttpResult updateBaseInfo(HttpServletRequest request, @RequestBody UserInfo userInfo) {
		return userSevice.updateBaseInfo(request,userInfo);
	}


	@RequestMapping("/login")
	@ResponseBody
	public HttpResult login(HttpServletRequest request, @RequestBody UserInfo user) {

		return userSevice.userLogin(request,user);
	}

	@RequestMapping("/loginOut")
	@ResponseBody
	public HttpResult loginOut(HttpServletRequest request) {

		return userSevice.loginOut(request);

	}

	@RequestMapping("/logOff")
	@ResponseBody
	public HttpResult logOff(HttpServletRequest request) {

		return userSevice.logOff(request);

	}


	//我的积分记录
	@RequestMapping("/pointDetails")
	@ResponseBody
	public HttpResult pointDetails(HttpServletRequest request,@RequestBody ComposeQueryDto qto) {
		return userSevice.pointDetails(request,qto);
	}

	//我的积分
	@RequestMapping("/myPoint")
	@ResponseBody
	public HttpResult myPoint(HttpServletRequest request) {
		return userSevice.myPoint(request);
	}

	//我的余额记录
	@RequestMapping("/balanceDetails")
	@ResponseBody
	public HttpResult balanceDetails(HttpServletRequest request,@RequestBody ComposeQueryDto qto) {
		return userSevice.balanceDetails(request,qto);
	}
	@RequestMapping("/myBalance")
	@ResponseBody
	public HttpResult myBalance(HttpServletRequest request) {
		return userSevice.myBalance(request);
	}


	@RequestMapping("/myBank")
	@ResponseBody
	public HttpResult myBank(HttpServletRequest request) {
		return userSevice.myBank(request);
	}

	@RequestMapping("/addBank")
	@ResponseBody
	public HttpResult addBank(HttpServletRequest request, @RequestBody UserBank bank) {
		if(StringUtils.isEmpty(bank.getBankNo())||StringUtils.isEmpty(bank.getBankName())||StringUtils.isEmpty(bank.getBankPersonName())){
			return HttpResult.PARAM_ERROR;
		}
		return userSevice.addBank(request,bank);
	}

	@RequestMapping("/updateBank")
	@ResponseBody
	public HttpResult updateBank(HttpServletRequest request, @RequestBody UserBank bank) {
		if(StringUtils.isEmpty(bank.getBankNo())||StringUtils.isEmpty(bank.getBankId())
				||StringUtils.isEmpty(bank.getBankName())||StringUtils.isEmpty(bank.getBankPersonName())){
			return HttpResult.PARAM_ERROR;
		}
		return userSevice.updateBank(request,bank);
	}


	//佣金体现申请
	@RequestMapping("/myDrawal")
	@ResponseBody
	public HttpResult myDrawal(HttpServletRequest request, @RequestBody ComposeQueryDto queryDto) {
		return userSevice.myDrawal(request,queryDto);
	}

	//佣金体现申请
	@RequestMapping("/withdrawal")
	@ResponseBody
	public HttpResult withdrawal(HttpServletRequest request, @RequestBody UserWithdrawalRecord drawal) {
		if(StringUtils.isEmpty(drawal.getBankId())||null==drawal.getWithdrawalMoney()){
			return HttpResult.PARAM_ERROR;
		}
		return userSevice.withdrawal(request,drawal);
	}


	//绑定上级
	@RequestMapping("/setParent")
	@ResponseBody
	public HttpResult setParent(HttpServletRequest request, @RequestBody UserInfo userInfo) {
		if(StringUtils.isEmpty(userInfo.getParentUserId())){
			return HttpResult.PARAM_ERROR;
		}

		return userSevice.setParent(request,userInfo);
	}

	//绑定上级
	@RequestMapping("/getNext")
	@ResponseBody
	public HttpResult getNext(HttpServletRequest request, @RequestBody ComposeQueryDto queryDto) {
		return userSevice.getNext(request,queryDto);
	}



	//消息
	@RequestMapping("/message")
	@ResponseBody
	public HttpResult message(HttpServletRequest request, @RequestBody ComposeQueryDto queryDto) {
		return userSevice.message(request,queryDto);
	}
	@RequestMapping("/messageDetail")
	@ResponseBody
	public HttpResult messageDetail(HttpServletRequest request, @RequestBody UserMessage message) {
		return userSevice.messageDetail(request,message);
	}
}
